﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using DataAccess;
using Util.PageInfo;
using ValidateCommon;

namespace RichECMS.ServerPages.Links
{
    public partial class AddLink : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (Request["Section"] != null)
                //{
                //    if (ParamAuth.IsNumber(Request["Section"].ToString()))
                //    {
                //        State.strWhereSql = "";
                //        State.intPageIndex = 1;
                //        State.intSTID = int.Parse(Request["Section"].ToString());
                //        this.btnAdd.HRef = this.btnAdd.HRef + "?Section=" + State.intSTID.ToString();
                //        this.btnSection.HRef = this.btnSection.HRef + "?Section=" + State.intSTID.ToString();
                //        InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
                //        AspNetPagerList.CurrentPageIndex = State.intPageIndex;
                //        LitPaagePath.Text = PageInfo.ShowPagePath(State.intSTID);
                //    }
                //    else
                //    {
                //        Alert("进入此页面的方式不正确");
                //        return;
                //    }
                //}
                //else
                //{
                //    Alert("进入此页面的方式不正确");
                //    return;
                //}
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        private void InitDataBind(int intCurrentPage, int intPageSize)
        {
            try
            {
                using (ArticleFactory objFactory = new ArticleFactory())
                {
                    StringBuilder strWhere = new StringBuilder();
                    ArtSecRelationFactory objArtSecRelationFactory = new ArtSecRelationFactory();
                    BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objArtSecRelationFactory.NewInstance();
                    int intPageFrom = intCurrentPage * intPageSize;
                    int intTotalCounts = 0;
                    SectionsFactory objSectionFactory = new SectionsFactory();
                    BusinessLogic.BusinessRules.Sections objSections = objSectionFactory.Creator(State.intSTID);
                    if (State.strWhereSql.Trim().Length == 0)
                    {
                        intTotalCounts = objArtSecRelation.GetTotalCount("intSectionID=" + State.intSTID);
                    }
                    else
                    {
                        intTotalCounts = objSections.GetTotalCountByCustomSql("SELECT count(*) FROM (select Top " + (intPageFrom).ToString() + " Article.* from Article inner  join ArtSecRelation on Article.intArticleID = ArtSecRelation.intArticleID" + " where ArtSecRelation.intSectionID=" + State.intSTID.ToString() + State.strWhereSql + " order by Article.intSort desc ,Article.dtmInTime desc,Article.intArticleID desc)t ");
                    }
                    //显示版块名称
                    LitPageName.Text = objSections.DataItem.chvSTCName;
                    LitPageCurrent.Text = intCurrentPage.ToString();
                    LitPageCount.Text = intTotalCounts.ToString();
                    if (intTotalCounts % intPageSize != 0)
                    {
                        if ((intCurrentPage == intTotalCounts / intPageSize + 1))
                        {
                            intPageFrom = (intCurrentPage - 1) * intPageSize;
                            //intPageSize =Model.Config.JudgeDatabase( intTotalCounts % intPageSize);//处理不同数据库差异
                            intPageSize = intTotalCounts % intPageSize;
                            intPageFrom = intPageFrom + intPageSize;
                        }
                    }
                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.* from Article inner  join ArtSecRelation on Article.intArticleID = ArtSecRelation.intArticleID" + " where ArtSecRelation.intSectionID=" + State.intSTID.ToString() + State.strWhereSql + " order by Article.intSort desc ,Article.dtmInTime desc,Article.intArticleID desc)t order by intSort asc, dtmInTime asc ,intArticleID asc)m " + " order by intSort asc, dtmInTime asc ,intArticleID asc)mm  order by intSort desc, dtmInTime desc ,intArticleID desc");
                    BusinessLogic.BusinessRules.Article objArticle = objFactory.CreatorByFullSql(strWhere.ToString());
                    if (objArticle != null)
                    {
                        rptList.DataSource = objArticle.Data.Article.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = intTotalCounts;
                        rptList.DataBind();
                    }
                    else
                    {
                        rptList.DataSource = objFactory.NewInstance().Data.Article.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = 0;
                        rptList.DataBind();
                        //Alert("暂无数据！");
                    }
                    objSectionFactory.Dispose();
                    objArtSecRelationFactory.Dispose();
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButtonsearch(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbWhere = new StringBuilder();
            if (txtTitle.Value.Trim() != "")
            {
                sbWhere.AppendFormat(" and Article.chvTitle like '%{0}%'", txtTitle.Value.Trim());
            }

            State.strWhereSql = sbWhere.ToString();
            State.intPageIndex = 1;
            InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSort_Click(object sender, ImageClickEventArgs e)
        {
            if (SortHidden.Value != "")
            {
                Response.Redirect("ArticleSort.aspx?SortID=" + SortHidden.Value);
            }
            else
            {
                Alert("请选中要排序的文章！");
            }
        }

        /// <summary>
        /// 删除所选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteAll_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                using (DBManager objDBManager = new DBManager())
                {
                    objDBManager.BeginTransaction();
                    try
                    {
                        ArticleFactory objFactory = new ArticleFactory(objDBManager);
                        objFactory.DeleteArticleWhere(" intArticleID in (" + SortHidden.Value.Trim() + ")");
                        //插入关联表
                        ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                        BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.NewInstance();
                        objAsFactory.DeleteWhere(" intArticleID in (" + SortHidden.Value.Trim() + ")");
                        //提交
                        objDBManager.Commit();
                        objAsFactory.Dispose();
                        objFactory.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 批量放入回收站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecovery_Click(object sender, ImageClickEventArgs e)
        {
        }

        /// <summary>
        /// 分页方法
        /// </summary>
        private void BindToPager()
        {
        }

        /// <summary>
        /// 分页  PageChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPagerArticle_PageChanged(object sender, EventArgs e)
        {
            State.intPageIndex = AspNetPagerList.CurrentPageIndex;
            InitDataBind(AspNetPagerList.CurrentPageIndex, AspNetPagerList.PageSize);
        }

        /// <summary>
        /// rptList_ItemCommand  编辑   放入回收站  删除
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                using (DBManager objDBManager = new DBManager())
                {
                    int intArticleId = Convert.ToInt32(e.CommandArgument);
                    objDBManager.BeginTransaction();
                    try
                    {
                        ArticleFactory objFactory = new ArticleFactory(objDBManager);
                        objFactory.DeleteArticle(intArticleId);
                        //插入关联表
                        ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                        BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.NewInstance();
                        objAsFactory.DeleteWhere(" intArticleID =" + intArticleId + " ");
                        //提交
                        objDBManager.Commit();
                        objAsFactory.Dispose();
                        objFactory.Dispose();
                        AlertAndRedirect("删除成功", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            //放入回收站
            if (e.CommandName == "Recycle")
            {
            }
        }
    }
}